<?php

namespace app\admin\model\zhongtai;

use think\Model;

class Recharge extends Model
{
// 表名
    protected $table = 'recharge';
    // 设置当前模型的数据库连接
    protected $connection = 'db_zhongtai';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $deleteTime = false;

    /**
     * 获取最新的更新时间
     */
    public function getUpTime($manage_id)
    {
        $updatetime = $this->where('manage_id', $manage_id)->order("zt_time desc")->value("zt_time");
        return $updatetime ?? 0;
    }

    public function upData($data, $manage_id)
    {
        if ($data) {
            $con = count($data);
            foreach ($data as $k => $v) {
                echo $k . PHP_EOL;
                if (($con - 1) == $k) {
                    $zttime = date('Y-m-d H:i:s',strtotime($v['zt_time'])+1);
                }else{
                    $zttime = $v['zt_time'];
                }
                $user_data = (new User())->getUserStatus($manage_id,$v['user_id']);
                if(!$user_data) continue;
                $in_data = [
                    "manage_id" => $manage_id,
                    "order_sn" => $v['order_sn'],
                    "user_id" => $v['user_id'],
                    "recharge_id" => $v['id'],
                    "status" => $v['status'],
                    "total_fee" => $v['total_fee'],
                    "pay_fee" => $v['pay_fee'],
                    "transaction_id" => $v['transaction_id'],
                    "payment_json" => $v['payment_json'],
                    "pay_type" => $v['pay_type'],
                    "paytime" => $v['paytime'],
                    "xw_orderNo" => $v['xw_orderNo'],
                    "xw_account" => $v['xw_account'],
                    "xw_code" => $v['xw_code'],
                    "mchId" => $v['mchId'],
                    "chnltrxid" => $v['chnltrxid'],
                    "xw_pay_url" => $v['xw_pay_url'],
                    "xw_is_default" => $v['xw_is_default'],
                    "xw_create_time" => $v['xw_create_time'],
                    "xw_query_num" => $v['xw_query_num'],
                    "ip" => $v['ip'],
                    "province" => $v['province'],
                    "city" => $v['city'],
                    "is_share" => $v['is_share'],
                    "is_fen" => $v['is_fen'],
                    "have_fee" => $v['have_fee'],
                    "sharableAmount" => $v['sharableAmount'],
                    "isNeedShare" => $v['isNeedShare'],
                    "is_check" => $v['is_check'],
                    "check_amount" => $v['check_amount'],
                    "check_json" => $v['check_json'],
                    "no_buy" => $v['no_buy'],
                    "createtime" => $v['createtime'],
                    "zt_time" => $zttime,
                ];
                $user_data = $this->where([
                    'recharge_id' =>  $v['id'],
                    'manage_id' => $manage_id,
                ])->find();
                if ($user_data) {
                    $this->where([
                        'recharge_id' =>  $v['id'],
                        'manage_id' => $manage_id,
                    ])->update($in_data);
                } else {
                    $this->insert($in_data);
                }
            }
        }
        echo "recharge表更新完成";
    }
}
